package com.acargil.sudoku.view.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.acargil.sudoku.model.ISudokuEngine;
import com.acargil.sudoku.view.ISudokuView;
import com.acargil.sudoku.view.util.MessageFactory;

public class ExitListener implements ActionListener {

	private static ActionListener instance = null;
	private ISudokuEngine model;
	private ActionListener saveListener;
	
	public static ActionListener getInstance(ISudokuEngine model, ISudokuView view) {
		if (instance == null) {
			instance = new ExitListener(model, view);
		}
		
		return instance;
	}
	
	public ExitListener(ISudokuEngine model, ISudokuView view) {
		this.model = model;
		this.saveListener = SaveListener.getInstance(model, view);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (model.isAnyCellFilled()) {
			String question, title;
			question = "Do you want to save the game before exit?";
			title = "Save Game?";
			boolean selection = MessageFactory.yesNoDialog(question, title);
			if (selection) {
				saveListener.actionPerformed(null);
			}
		} else {
			System.exit(1);
		}
	}

}
